package com.sr.examination.category.history;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sr.examination.R;
import com.sr.examination.common.event.CollectionEvent;
import com.sr.examination.exam.exercise.ExerciseActivity;
import com.sr.examination.net.Net;
import com.tl.commonlibrary.event.EventUtil;
import com.tl.commonlibrary.network.ErrorResponse;
import com.tl.commonlibrary.network.RequestListener;
import com.tl.commonlibrary.network.bean.base.BaseBean;
import com.tl.commonlibrary.tool.NumberUnit;
import com.tl.commonlibrary.tool.UIUtils;
import com.tl.commonlibrary.ui.BaseListViewFragmentActivity;
import com.tl.commonlibrary.ui.adapter.ViewHolder;
import com.tl.commonlibrary.ui.beans.CollectionBean;
import com.tl.commonlibrary.ui.beans.ExerciseListBean;
import com.tl.commonlibrary.ui.beans.HistoryBean;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import retrofit2.Call;

/**
 * Created by jiangzhongshan on 18/2/4.
 * 收藏
 */

public class HistoryActivity extends BaseListViewFragmentActivity<HistoryBean> {
    public static void start(Context context) {
        Intent starter = new Intent(context, HistoryActivity.class);
        context.startActivity(starter);
    }

    private NumberUnit numberUnit;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EventUtil.register(this);
        numberUnit = new NumberUnit().setFormatDot(false);
        setTitle(getString(R.string.exam_history02));
        setListViewPadding(0, (int) getResources().getDimension(R.dimen.base_spacing_20px), 0, 0);
        setListDividerHeightTransparent((int) getResources().getDimension(R.dimen.divider_height));
        autoRefresh();
    }

    /**
     * list item的布局文件
     *
     * @return
     */
    @Override
    public int getItemLayoutResId() {
        return R.layout.item_history;
    }

    /**
     * 数据请求
     *
     * @param isRefresh
     */
    @Override
    public void requestData(final boolean isRefresh) {
        dismissNoData();
        Net.getHistoryList(page, new RequestListener<BaseBean<ArrayList<HistoryBean>>>() {
            @Override
            public void onSuccess(Call<BaseBean<ArrayList<HistoryBean>>> call, BaseBean<ArrayList<HistoryBean>> response) {
                if (response.isSuccessful()) {
                    datas = response.data;
                    if (isRefresh) {
                        if (datas == null || datas.isEmpty()) {
                            listView.setAdapter(null);
                            showNoDataView(getString(R.string.no_data_exercise_history_list));
                            requestFailed();
                        } else {
                            setDatas(datas);
                            page++;
                            requestSuccess();
                        }
                    } else {
                        if (datas == null || datas.isEmpty()) {
                            addDatas(datas);
                            page++;
                            requestSuccess();
                        } else {
                            requestFailed();
                            UIUtils.showToast(R.string.all_data_loaded);
                        }
                    }
                }
            }

            @Override
            public void onFailed(Call<BaseBean<ArrayList<HistoryBean>>> call, ErrorResponse errorResponse) {
                requestFailed();
            }
        });
    }

    private void requestDetail(final HistoryBean historyBean) {
        if (historyBean == null) {
            return;
        }
        showProgressDialog();
        Net.getHistoryDetail(historyBean.getPUserSummaryId(), new RequestListener<BaseBean<ExerciseListBean>>() {
            @Override
            public void onSuccess(Call<BaseBean<ExerciseListBean>> call, BaseBean<ExerciseListBean> response) {
                if (response.isSuccessful()) {
                    ExerciseActivity.start(context, response.data.getPaperTitle(), historyBean.getPaperId(), ExerciseListBean.TYPE_EXERCISE_HISTORY, response.data);
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(Call<BaseBean<ExerciseListBean>> call, ErrorResponse errorResponse) {
                dismissProgressDialog();
            }
        });
    }

    /**
     * 点击了item
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HistoryBean historyBean = adapter.getItem(position);
        requestDetail(historyBean);
    }

    /**
     * 点击了item中某一个子控件，
     *
     * @param v
     */
    @Override
    public void onClickViewInListItem(View v) {

    }
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onEvent(CollectionEvent collectionEvent) {
//        if (controller != null && !controller.isRefreshing()) {
//            controller.autoRefresh();
//        }
//    }

    /**
     * Adapter的getView
     *
     * @param viewHolder
     * @param entity
     * @param position
     * @param convertView
     * @param isConvert
     * @param itemViewType
     * @param parent
     */
    @Override
    public void getView(ViewHolder viewHolder, HistoryBean entity, int position, View convertView, boolean isConvert, int itemViewType, ViewGroup parent) {
        TextView titleTView = viewHolder.getView(R.id.titleTView);
        TextView scoreTView = viewHolder.getView(R.id.scoreTView);
        TextView dateTView = viewHolder.getView(R.id.dateTView);
        titleTView.setText(entity.getPaperTitle());
        numberUnit.set(entity.getTotalScore());
        scoreTView.setText(getString(R.string.unit_score_pre, numberUnit.get2F()));
        dateTView.setText(entity.getSubmittedTime());
    }

    @Override
    public void onNoDataClicked() {
        super.onNoDataClicked();
        controller.autoRefresh();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onFragmentTrigger(int tag, Bundle bundle) {

    }
}
